package cn.zhangxin.pethome.system.product.service.impl;

import cn.zhangxin.pethome.constants.BaseConstants;
import cn.zhangxin.pethome.system.product.domain.Product;
import cn.zhangxin.pethome.system.product.domain.ProductDetail;
import cn.zhangxin.pethome.system.product.mapper.ProductDetailMapper;
import cn.zhangxin.pethome.system.product.mapper.ProductMapper;
import cn.zhangxin.pethome.system.product.service.IProductService;
import cn.zhangxin.pethome.base.service.impl.BaseServiceImpl;
import cn.zhangxin.pethome.utils.AuthService;
import cn.zhangxin.pethome.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.bind.Element;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zx
 * @since 2023-02-09
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private AuthService authService;
    @Override
    public void delete(Long id) {
        Product one = productMapper.findOne(id);
        if (one.getState()==BaseConstants.Serve.STATE_ON){
            throw new BusinessException("上架的服务不能删除");
        }
        productDetailMapper.delete(productDetailMapper.findByProductId(id).getId());
        productMapper.delete(id);
    }

    @Override
    public void add(Product product) {
        product.setCreatetime(new Date());//添加创建时间
        product.setState(BaseConstants.Serve.STATE_DOWN);//设置状态为下架
        productMapper.add(product);//添加获得id
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.add(detail);//添加详细表

    }

    //上架
    @Override
    public void onsale(List<Long> ids) {
            StringBuffer msg = new StringBuffer();
            for (Long id : ids) {
                Product one = productMapper.findOne(id);
                if(authService.authentication(one.getName())){//上架判断名字合法性
                    one.setState(BaseConstants.Serve.STATE_ON);
                    one.setOnsaletime(new Date());
                    productMapper.update(one);
                }else {
                    msg.append(one.getName()).append(" ");
                }
            }
            if (msg.length()>0){
                throw new BusinessException(msg.toString());
            }

    }

    //下架
    @Override
    public void offsale(List<Long> ids) {
        for (Long id : ids) {
            Product one = productMapper.findOne(id);
            one.setOffsaletime(new Date());
            one.setState(BaseConstants.Serve.STATE_DOWN);
            productMapper.update(one);
        }
    }

    @Override
    public Product productWeb(Long id) {
        Product one = productMapper.findOne(id);
        ProductDetail byProductId = productDetailMapper.findByProductId(id);
        one.setDetail(byProductId);
        return one;
    }

    @Override
    public void update(Product product) {
        if (product.getState()==BaseConstants.Serve.STATE_ON){
            throw new BusinessException("上架的服务不能修改");
        }
        if (product.getName()==null){
            throw new BusinessException("名字不能为空");
        }
        if (product.getResources() == null){
            throw new BusinessException("请至少上传一张图片");
        }
        if(!authService.authentication(product.getName())){
            throw new BusinessException("名字违规");
        }
        ProductDetail detail = product.getDetail();
        ProductDetail byProductId = productDetailMapper.findByProductId(product.getId());
        byProductId.setIntro(detail.getIntro());
        byProductId.setOrderNotice(detail.getOrderNotice());
        productDetailMapper.update(byProductId);
        super.update(product);
    }
}
